/************************************************************
 * Project Name         [Thinking_In_Java]
 * File Name            [Example_07_Object.java]
 * Creation Date        [18-Jun-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_02_oop;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Object类的equals, hashCode, toString方法
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Example_07_Object {

}

/**
 * Person
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
class Person extends Object implements Cloneable {

	private String id;
	private String name;
	private char gender;
	private Date birthday;

	/**
	 * 默认构造方法
	 */
	public Person() {
		super();
	}

	/**
	 * 使用ID构建对象
	 * 
	 * @param id
	 */
	public Person(String id) {
		this(id, "No Name", 'F', new Date());
	}

	/**
	 * 构造方法
	 * 
	 * @param id
	 *            id
	 * @param name
	 *            姓名
	 * @param gender
	 *            性别
	 * @param birthday
	 *            生日
	 */
	public Person(String id, String name, char gender, Date birthday) {
		super();
		this.id = id;
		this.name = name;
		this.gender = gender;
		this.birthday = birthday;
	}

	/**
	 * 获取ID
	 * 
	 * @return id
	 */
	public String getId() {
		return id;
	}

	/**
	 * 设置ID
	 * 
	 * @param id
	 *            id的值
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * 获取名字
	 * 
	 * @return 名字
	 */
	public String getName() {
		return name;
	}

	/**
	 * 设置名字
	 * 
	 * @param name
	 *            要设置的名字
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * 获得性别
	 * 
	 * @return 性别的值
	 */
	public char getGender() {
		return gender;
	}

	/**
	 * 设置性别
	 * 
	 * @param gender
	 *            要设置的性别
	 */
	public void setGender(char gender) {
		this.gender = gender;
	}

	/**
	 * 获得生日
	 * 
	 * @return 生日的值
	 */
	public Date getBirthday() {
		return birthday;
	}

	/**
	 * 设置生日
	 * 
	 * @param birthday
	 *            要设置的生日日期
	 */
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	/**
	 * hashCode方法
	 * 
	 * @return hashCode
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
		// JDK 7
		// return Objects.hash(id, name);
	}

	/**
	 * 自定义equals()方法,id和name相同即为同一个人
	 * 
	 * @param obj
	 *            要比较的对象
	 * 
	 * @return 是否为同一个人
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Person other = (Person) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
		// JDK 7
		// return Objects.equals(this.id, other.id)
		// && Objects.equals(this.name, other.name);
	}

	/**
	 * 返回对象的字符串表示
	 * 
	 * @return 对象的字符串表示
	 */
	@Override
	public String toString() {
		return "id = " + id + "\tname = " + name + "\tgender = " + gender
				+ "\tbirthday = "
				+ new SimpleDateFormat("yyyy年MM月dd日").format(birthday);
	}

	/**
	 * 垃圾回收方法
	 * 
	 * @throws Throwable
	 */
	@Override
	protected void finalize() throws Throwable {
		System.out.println("finalize method is called...");
	}

	/**
	 * clone对象
	 * 
	 * @return 克隆得到的新对象
	 */
	@Override
	protected Object clone() throws CloneNotSupportedException {
		Person cloned = new Person();
		cloned = (Person) super.clone();
		cloned.birthday = (Date) birthday.clone();
		return cloned;
	}
}
